package cn.jhc.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import cn.jhc.bean.Author;
import cn.jhc.bean.Publisher;
import cn.jhc.factory.ConnectionFactory;

public class PublisherDAO {
	public List<Publisher> getAll(){
		List<Publisher> list = new ArrayList<>();
		Connection connection = ConnectionFactory.getConnection();
		String sql = "select publisherID,publisherName from publishers";
		Statement statement = null;
		ResultSet rs = null;

		try {
			statement = connection.createStatement();
			rs = statement.executeQuery(sql);
			while (rs.next()) {
				Publisher p = new Publisher();
				p.setId(rs.getInt(1));
				p.setName(rs.getString(2));
				list.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
				statement.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	public void addPublisher(Publisher publisher) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		final String sql = "insert into publishers values(?,?)"; 
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, publisher.getId());
			ps.setString(2, publisher.getName());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void deletePublisher(int id) {
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "delete from publishers where publisherID=?";
		try {
			ps = connection.prepareStatement(sql);
			ps.setInt(1, id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				ps.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public Publisher findByName(String name) {
		Publisher p = new Publisher();
		Connection connection = ConnectionFactory.getConnection();
		Statement stat= null;
		ResultSet rs = null;
		String sql = "select publisherID from publishers where publisherName='"+name+"'";
		try {
			stat = connection.createStatement();
			rs = stat.executeQuery(sql);
			if(rs.next()){
				p.setId(rs.getInt(1));
				p.setName(name);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				stat.close();
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return p;
	}
}
